package cn.iocoder.yudao.module.exam.service.paperquestion;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.exam.controller.admin.paperquestion.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.paperquestion.PaperQuestionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.paperquestion.PaperQuestionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.exam.enums.ErrorCodeConstants.*;

/**
 * 试卷试题 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PaperQuestionServiceImpl implements PaperQuestionService {

    @Resource
    private PaperQuestionMapper paperQuestionMapper;

    @Override
    public Long createPaperQuestion(PaperQuestionSaveReqVO createReqVO) {
        // 插入
        PaperQuestionDO paperQuestion = BeanUtils.toBean(createReqVO, PaperQuestionDO.class);
        paperQuestionMapper.insert(paperQuestion);
        // 返回
        return paperQuestion.getId();
    }

    @Override
    public void updatePaperQuestion(PaperQuestionSaveReqVO updateReqVO) {
        // 校验存在
        validatePaperQuestionExists(updateReqVO.getId());
        // 更新
        PaperQuestionDO updateObj = BeanUtils.toBean(updateReqVO, PaperQuestionDO.class);
        paperQuestionMapper.updateById(updateObj);
    }

    @Override
    public void deletePaperQuestion(Long id) {
        // 校验存在
        validatePaperQuestionExists(id);
        // 删除
        paperQuestionMapper.deleteById(id);
    }

    private void validatePaperQuestionExists(Long id) {
        if (paperQuestionMapper.selectById(id) == null) {
            throw exception(PAPER_QUESTION_NOT_EXISTS);
        }
    }

    @Override
    public PaperQuestionDO getPaperQuestion(Long id) {
        return paperQuestionMapper.selectById(id);
    }

    @Override
    public PageResult<PaperQuestionDO> getPaperQuestionPage(PaperQuestionPageReqVO pageReqVO) {
        return paperQuestionMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveQuestions(Long paperId, List<PaperQuestionDO> paperQuestionDOList) {
        for (int i = 0; i < paperQuestionDOList.size(); i++) {
            paperQuestionDOList.get(i).setSortNo(i+1);
            paperQuestionDOList.get(i).setPaperId(paperId);
        }
        List<PaperQuestionDO> insertQs = paperQuestionDOList.stream().filter(qdo -> qdo.getId() == null).collect(Collectors.toList());
        List<PaperQuestionDO> updateQs = paperQuestionDOList.stream().filter(qdo -> qdo.getId() != null).collect(Collectors.toList());
         if(!CollectionUtils.isEmpty(insertQs)){
            paperQuestionMapper.insertBatch(insertQs,500);
        }
        if(!CollectionUtils.isEmpty(updateQs)){
            paperQuestionMapper.delete(Wrappers.<PaperQuestionDO>lambdaQuery().eq(PaperQuestionDO::getPaperId,paperId).notIn(PaperQuestionDO::getId,updateQs.stream().map(PaperQuestionDO::getId).collect(Collectors.toSet())));
            paperQuestionMapper.updateBatch(updateQs,500);
        }
    }

    @Override
    public List<PaperQuestionDO> findQuestionsByPaperId(Long paperId) {
        List<PaperQuestionDO> paperQuestionDOS = paperQuestionMapper.selectList(PaperQuestionDO::getPaperId, paperId);
        paperQuestionDOS.sort(Comparator.comparing(PaperQuestionDO::getSortNo));
        return paperQuestionDOS;
    }

}